﻿using DevComponents.DotNetBar;
using ZhiKeDuoServer.Model;
using Maticsoft.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AppCash
{
    public partial class frmMoney : Form
    {
        ZhiKeDuoServer.BLL.Base<Payment> Bll = new ZhiKeDuoServer.BLL.Base<Payment>();
        int print = 0;
        public frmMoney()
        {
            InitializeComponent();
            listBox1.SelectedIndex = 1;
            print = 0;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:

                    this.Close();
                    return true;
                case Keys.Enter:
                    tryCash();
                    return true;
                case Keys.D:

                    return true;
                case Keys.F1:
                    Form frmToday = new frmToday();
                    frmToday.ShowDialog();
                    return true;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        private void frmMoney_Load(object sender, EventArgs e)
        {
            this.Show();
            this.TopMost = true;
            tbYS.Text = string.Format("{0:F2}", double.Parse(this.Tag.ToString()));


            listBox1.DisplayMember = "Name";
            listBox1.ValueMember = "Id";
            listBox1.DataSource = Bll.GetListByPage("", "sort asc ", 30, 1);

            listBox1.Focus();
            labelERR.Text = "";
        }

        private void tbSSJE_TextChanged(object sender, EventArgs e)
        {
            tbZL.Text = "";
        }
        protected void tryCash()
        {

            int id = Convert.ToInt32(listBox1.SelectedValue);
            var payment = Bll.GetModel(id);
            if (payment.Online > 0)
            {
                if (txtsm.Text != "")
                {
                    if (tbZL.Text.Trim() == "")
                    {
                        tbZL.Text = string.Format("{0:F2}", (double.Parse(tbSSJE.Text.Trim()) - double.Parse(tbYS.Text.Trim())));
                    }
                    WebClient wc = new WebClient();
                    //wc.RequestHeaders.Add("shop", ZhiKeDuoServer.Model.GlobalsInfo.Shop.Shop.ToString());

                    string dat = wc.PostJson(payment.Url + txtsm.Text, "");//"https://next.zhikeduo.cn/api/pay/qr_pay/"+ txtsm.Text, "");

                    Cash(id, "N");
                }
            }
            else
            {
                Cash(id);
            }
        }



        protected void Cash(int paymentid, String Paystatus = "y")
        {
            if (tbSSJE.Text.Trim() == "")
            {
                tbSSJE.Select();
                return;
            }
            if (tbZL.Text.Trim() == "")
            {
                tbZL.Text = string.Format("{0:F2}", (double.Parse(tbSSJE.Text.Trim()) - double.Parse(tbYS.Text.Trim())));
            }
            else
            {
                if (print == 0)
                {
                    decimal strZL = decimal.Parse(tbZL.Text.Trim());
                    if (strZL < 0)
                    {
                        labelERR.Text = ("实际收款不足");
                        return;
                    }

                    labelERR.Text = "";
                    ZhiKeDuoServer.BLL.GoodsInfo bGoods = new ZhiKeDuoServer.BLL.GoodsInfo();
                    ZhiKeDuoServer.BLL.SaleInfo bSale = new ZhiKeDuoServer.BLL.SaleInfo();
                    ZhiKeDuoServer.BLL.Base<Cart> bcat = new ZhiKeDuoServer.BLL.Base<Cart>();
                    ZhiKeDuoServer.Model.SaleInfo mSale = new ZhiKeDuoServer.Model.SaleInfo();
                    frmCash frmP = (frmCash)this.Owner;

                    DataGridView dgv = (DataGridView)frmP.Controls.Find("dGV", true)[0];
                    if (dgv.Rows.Count > 0)
                    {
                        PrintOrder(dgv);

                        //  double price = double.Parse(dgv.Rows[i].Cells[4].Value.ToString());
                        mSale.Pid = ZhiKeDuoServer.Model.GlobalsInfo.bill;
                        // mSale.GoodsCode = dgv.Rows[i].Cells[0].Value.ToString();
                        // mSale.Counts = int.Parse(dgv.Rows[i].Cells[3].Value.ToString());
                        //mSale.Price = price;
                        mSale.PriceSum = double.Parse(tbYS.Text);
                        mSale.VipCode = ZhiKeDuoServer.Model.GlobalsInfo.vipCode;
                        mSale.Oper = ZhiKeDuoServer.Model.GlobalsInfo.UserName;
                        mSale.zhekou = ZhiKeDuoServer.Model.GlobalsInfo.vipZK;
                        mSale.banci = ZhiKeDuoServer.Model.GlobalsInfo.banci;
                        mSale.Paytype = paymentid;
                        mSale.Paystatus = Paystatus;
                        mSale.IDate = DateTime.Now;
                        mSale.Shop = ZhiKeDuoServer.Model.GlobalsInfo.Shop.Shop;
                        bSale.Add(mSale);
                    }
                    else
                    {
                        labelERR.Text = ("没有需要付款的商品");
                        return;
                    }
                    mSale = bSale.GetModel(ZhiKeDuoServer.Model.GlobalsInfo.bill);
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        Decimal price = Decimal.Parse(dgv.Rows[i].Cells[4].Value.ToString());
                        bcat.Add(new Cart
                        {
                            Goodscode = dgv.Rows[i].Cells[0].Value.ToString(),
                            Price = price,
                            Counts = int.Parse(dgv.Rows[i].Cells[3].Value.ToString()),
                            Sale_id = mSale.Id,
                            Pricehy = (price * (decimal)ZhiKeDuoServer.Model.GlobalsInfo.vipZK)

                        });

                        //减少货物质量
                        bGoods.UpdateCount(0 - int.Parse(dgv.Rows[i].Cells[3].Value.ToString()), dgv.Rows[i].Cells[0].Value.ToString());
                    }
                    if (strZL == 0)
                    {
                        ResetAndClose();
                    }
                    print++;
                }
                else
                {
                    ResetAndClose();
                }
            }


        }

        private void ResetAndClose()
        {

            frmCash frmP = (frmCash)this.Owner;
            ZhiKeDuoServer.Model.GlobalsInfo.vipZK = 1;
            ZhiKeDuoServer.Model.GlobalsInfo.vipCode = "";
            ZhiKeDuoServer.Model.GlobalsInfo.vipName = "";
            frmP.ClearForm();
            frmP.showPre();
            this.Close();
        }

        public void PrintOrder(DataGridView dgv)
        {

            if (dgv.Rows.Count > 0)
            {

                BeiYangOPOS opos = new BeiYangOPOS();

                bool b = opos.OpenPrinter(ZhiKeDuoServer.Model.GlobalsInfo.PrinterIPAddress);
                if (!b)
                {
                    LogManager.WriteLog("Error",
                        "初始化的打印机参数失败。请检测打印机配置。");
                }
                else
                {
                    BeiYangOPOS.POS_StartDoc();
                    //ZhiKeDuoServer.Model.GlobalsInfo.Shop.Name
                    BeiYangOPOS.POS_SetRightSpacing(0);
                    BeiYangOPOS.POS_SetLineSpacing(100);
                    BeiYangOPOS.POS_S_TextOut(ZhiKeDuoServer.Model.GlobalsInfo.Shop.Name, 50, 2, 3, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_FeedLines(2);
                    BeiYangOPOS.POS_SetLineSpacing(24);
                    BeiYangOPOS.POS_S_TextOut("名称", 5, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_S_TextOut("价格", 200, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_S_TextOut("数量", 320, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_FeedLines(1);
                    BeiYangOPOS.POS_S_TextOut("--------------------------------", 0, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_FeedLines(1);

                }



                for (int i = 0; i < dgv.Rows.Count; i++)
                {


                    BeiYangOPOS.POS_S_TextOut(dgv.Rows[i].Cells[1].Value.ToString(), 10, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_S_TextOut(dgv.Rows[i].Cells[4].Value.ToString(), 200, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                    BeiYangOPOS.POS_S_TextOut(dgv.Rows[i].Cells[3].Value.ToString(), 350, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);

                    BeiYangOPOS.POS_FeedLines(1);

                }
                BeiYangOPOS.POS_S_TextOut("--------------------------------", 0, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                BeiYangOPOS.POS_FeedLines(1);
                BeiYangOPOS.POS_S_TextOut("总计:" + tbYS.Text, 150, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                //BeiYangOPOS.POS_S_TextOut("总计:" + tbYS.Text, 200, 1, 1, opos.POS_FONT_TYPE_STANDARD, opos.POS_FONT_TYPE_CHINESE);
                BeiYangOPOS.POS_FeedLines(10);

                BeiYangOPOS.POS_CutPaper(0, 200);

                BeiYangOPOS.POS_KickOutDrawer(0x00, 10, 10);
                //BeiYangOPOS.POS_KickOutDrawer(0x01, 50, 50);
                BeiYangOPOS.POS_EndDoc();
                BeiYangOPOS.POS_Close();
            }



        }
        private void listBox1_Enter(object sender, EventArgs e)
        {
            listBox1.BackColor = System.Drawing.SystemColors.HotTrack;
        }

        private void listBox1_Leave(object sender, EventArgs e)
        {
            listBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
        }

        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedValue == null)
            {
                listBox1.SelectedIndex = 0;
                return;
            }
            int id = Convert.ToInt32(listBox1.SelectedValue);
            var payment = Bll.GetModel(id);
            if (payment.Online > 0)
            {
                txtsm.Visible = true;
                lbsm.Visible = true;

            }
            else
            {
                txtsm.Visible = !true;
                lbsm.Visible = !true;
            }
        }
    }
}
